<template>
  <div class="manage">
    <el-dialog
      title="药品管理"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- 表单信息 -->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="药品名称" prop="drugName">
          <el-input
            placeholder="请输入药品名称"
            v-model="form.drugName"
          ></el-input>
        </el-form-item>
        <el-form-item label="保质期" prop="warranty">
          <el-input
            placeholder="请输入保质期"
            v-model="form.warranty"
          ></el-input>
        </el-form-item>
        <el-form-item label="药品编码" prop="drugNo">
          <el-input
            placeholder="请输入药品编码"
            v-model="form.drugNo"
          ></el-input>
        </el-form-item>
        <el-form-item label="厂商" prop="sid">
          <el-select v-model="form.sid" placeholder="请选择厂商">
            <div v-for="spl in suppliers" :key="spl.sid">
              <el-option
                v-if="form.sid == spl.sid"
                :label="spl.splName"
                :value="spl.sid"
                selected
              ></el-option>
              <el-option
                v-else
                :label="spl.splName"
                :value="spl.sid"
              ></el-option>
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="进价" prop="drugPrice">
          <el-input
            placeholder="请输入进价"
            v-model="form.drugPrice"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button class="addBtn" type="primary" @click="handlerAdd">
        + Add
      </el-button>
      <el-form :model="searchForm">
        <el-form-item class="search">
          <el-input
            placeholder="请输入名称"
            v-model="searchForm.name"
          ></el-input>
          <el-button type="primary" @click="searchName">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <el-table height="90%" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="drugName" label="药品名"> </el-table-column>
        <el-table-column prop="splName" label="厂商名"> </el-table-column>
        <el-table-column prop="warranty" label="保质期"> </el-table-column>
        <el-table-column prop="drugNo" label="药品编码"> </el-table-column>
        <el-table-column prop="drugPrice" label="进价"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row.did)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pageData.total"
        @current-change="handlePage"
        :page-size="10"
        :current-page.sync="pageData.curPage"
        class="pager"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getDrug,
  addDrug,
  editDrug,
  delDrug,
  getSupplier,
} from "@/api/drugInfo/drug";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        did: "",
        drugName: "",
        warranty: "",
        drugNo: "",
        drugPrice: "",
        sid: "",
      },
      formNull: {
        did: "",
        drugName: "",
        warranty: "",
        drugNo: "",
        drugPrice: "",
        sid: "",
      },
      rules: {
        drugName: [
          { required: true, message: "请输入药品名称", trigger: "blur" },
        ],
        warranty: [
          { required: true, message: "请输入保质期", trigger: "blur" },
        ],
        drugNo: [
          { required: true, message: "请输入药品编码", trigger: "blur" },
        ],
        drugPrice: [{ required: true, message: "请输入进价", trigger: "blur" }],
        sid: [{ required: true, message: "请输入厂商名", trigger: "blur" }],
      },
      suppliers: [{ sid: "1", splName: "2332" }],
      tableData: [
        {
          drugName: "",
          splName: "",
          warranty: "",
          drugNo: "",
          drugPrice: "",
        },
      ],
      modelType: 0, //0表示新增 1表示编辑

      pageData: {
        total: 0, //当前总条数
        curPage: 1,
      },
      searchForm: {
        name: "",
      },
    };
  },
  methods: {
    //提交用户表单
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.modelType == 0) {
            //新增用户
            addDrug(this.form).then(() => {
              //发送后的操作
              this.getList();
            });
          } else {
            editDrug(this.form).then(() => {
              //更新后的操作
              this.getList();
            });
          }
          //修改页码为第一页
          this.pageData.curPage = 1;
          //关闭弹窗
          this.dialogVisible = false;
          this.$refs.form.resetFields();
        }
      });
    },
    handleClose() {
      // this.$refs.form.resetFields();
      this.form = this.formNull;
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.modelType = 1;
      this.dialogVisible = true;
      //深拷贝
    },
    handleDel(uid) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDrug(uid).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
            //修改页码为第一页
            this.pageData.curPage = 1;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getList(curPage, limit, sname = null) {
      if (curPage === null || limit == null) {
        curPage = 0;
        limit = 10;
      }
      getDrug({ curPage: curPage, limit: limit, sname: sname }).then(
        ({ data }) => {
          //获取数据
          const page = data.data;
          this.tableData = page.records;
          this.pageData.total = page.total;
        }
      );
    },
    searchName() {
      this.getList(null, null, this.searchForm.name);
    },
    handlerAdd() {
      this.modelType = 0;
      this.dialogVisible = true;
    },
    //选择页面的回调
    handlePage(val) {
      this.getList(val, 10);
    },
  },
  mounted() {
    this.getList();
    getSupplier().then(({ data }) => {
      this.suppliers = data.data;
    });
  },
};
</script>

<style lang="less">
.manage {
  height: 90%;
  .manage-header {
    display: flex;
    justify-content: space-between;
    .addBtn {
      height: 70%;
    }
  }
  .common-table {
    position: relative;
    height: calc(85%);
    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }

  .el-form-item__content {
    display: flex;
  }
}
</style>
